//  控制器模块
let { insertOne, delOne, getList } = require('../model/carousel');

let addCofn = async (req, res) => {
  // 获取上传文件的文件名
  let { is_show } = req.body;

  let name = req.file?.filename;
  if (!name) return res.send({ code: 0, message: '添加失败' });

  let r = await insertOne({ name, is_show });
  if (r.status === 0) return res.send({ code: 0, message: '添加失败' });
  res.send({ code: 1, message: '添加成功' });
}


let rmCofn = async (req, res) => {
  let { name } = req.query;
  let r = await delOne(name);
  if (r.status === 0) return res.send({ code: 0, message: '删除失败' });
  require('fs').unlinkSync(`./uploads/${name}`);
  res.send({ code: 1, message: '删除成功' });
}

let listCofn = async (req, res) => {

  let { is_show } = req.query;

  // 组装条件
  let cond = {};
  if (is_show === 1) cond.is_show = 1;
  //  获取
  let r = await getList(cond)
  if (r.status === 0) return res.send({ code: 0, message: '获取失败' })
  res.send({ code: 1, message: '获取成功', list: r })
}

// 导出
module.exports = {
  addCofn, rmCofn, listCofn
}